class Enterprise < ActiveRecord::Base    

  attr_protected []
    
  validates :name, 
    :presence => true,
    :uniqueness => true

  attr_protected []
    
  belongs_to :admin, :class_name => "User", :foreign_key => "admin_id"
  has_many :users
  has_many :groups
  
  before_save do |enterprise|
    enterprise.qq = enterprise.qq.sub(',',' ').sub('，',' ').squeeze(" ") if enterprise.qq.present?
  end
  
  def set_admin!(user)
    update_attributes :admin_id => user.id
  end

  def users
    User.where('enterprise_id =?', id)
  end
  
  def self.current
    User.current.try(:enterprise)
  end
  
  def self.current_id
    User.current.try(:enterprise_id)
  end
  
  def employ_current_user?
    id == Enterprise.current_id
  end

  def qqs
    (qq || '').split(' ')
  end
  
end
